package makabakaquestionnaire.zutsoftware.controller;

import makabakaquestionnaire.zutsoftware.annotation.Log;
import makabakaquestionnaire.zutsoftware.entity.Users;
import makabakaquestionnaire.zutsoftware.entity.page.TableDataInfo;
import makabakaquestionnaire.zutsoftware.enums.BusinessType;
import makabakaquestionnaire.zutsoftware.framework.manager.ExecutorManager;
import makabakaquestionnaire.zutsoftware.framework.manager.factory.TaskFactory;
import makabakaquestionnaire.zutsoftware.framework.security.Service.TokenService;
import makabakaquestionnaire.zutsoftware.framework.security.model.LoginUser;
import makabakaquestionnaire.zutsoftware.service.IUsersService;
import makabakaquestionnaire.zutsoftware.system.common.AjaxResult;
import makabakaquestionnaire.zutsoftware.system.common.BaseController;
import makabakaquestionnaire.zutsoftware.system.entity.SysRole;
import makabakaquestionnaire.zutsoftware.system.service.ISysRoleService;
import makabakaquestionnaire.zutsoftware.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户信息Controller
 *
 * @author wjf
 * @date 2022-03-10
 */
@RestController
@RequestMapping("/system/users")
public class UsersController extends BaseController {
    @Autowired
    private IUsersService usersService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:users:list')")
    @GetMapping("/list")
    public TableDataInfo list(Users users) {
        startPage();
        List<Users> list = usersService.selectUsersList(users);
        ExecutorManager.getExecutorManager().execute(TaskFactory.test());
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:users:export')")
//    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(Users users)
//    {
//        List<Users> list = usersService.selectUsersList(users);
//        ExcelUtil<Users> util = new ExcelUtil<Users>(Users.class);
//        return util.exportExcel(list, "users");
//    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:users:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        Users users = usersService.selectUsersById(id);
        ajax.put(AjaxResult.DATA_TAG,users);
        //查询所有角色，再进行权限判断，让非管理员无法修改为管理员
        List<SysRole> roles = roleService.selectRoleAll();
        long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getId();
        ajax.put("roles", Users.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("roleIds", users.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        //return AjaxResult.success(usersService.selectUsersById(id));
        return ajax;
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:users:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Users users) {
        return toAjax(usersService.insertUsers(users));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:users:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Users users) {
        return toAjax(usersService.updateUsers(users));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:users:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(usersService.deleteUsersByIds(ids));
    }

    /**
     * 根据token获取在线用户
     */
    @GetMapping("/getLoginUser")
    public AjaxResult getLoginUser() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("loginUser", loginUser);
        return ajax;
    }

}
